package com.example.onenetdht11;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.example.onenetdht11.dao.UserDao2;
import com.example.onenetdht11.entity.User2;

public class ReviseActivity extends AppCompatActivity {
    Button btnSaveInformation;
    User2 user;
    UserDao2 userDao;
    EditText etNickname, etUsername, etAge, etIDCard, etAddress;
    RadioGroup rgSex;
    RadioButton r;
    CheckBox cbIsOld,cbIsDisease,cbIsOther;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_revise);
        initView();
        initListener();
        initDate();
    }
    private void initView() {
        etUsername = findViewById(R.id.et_username);
        etAge = findViewById(R.id.et_age);
        etNickname = findViewById(R.id.et_nickname);
        etIDCard = findViewById(R.id.et_IDcard);
        etAddress = findViewById(R.id.et_address);
        rgSex = findViewById(R.id.rg_sex);
        btnSaveInformation = findViewById(R.id.btn_save_information);
        cbIsOld = findViewById(R.id.cb_isOld);
        cbIsDisease = findViewById(R.id.cb_isDisease);
        cbIsOther = findViewById(R.id.cb_isOther);
    }

    private void initListener() {
        View.OnClickListener listener = new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.btn_save_information) {
                    if (cbIsOld.isChecked())
                        user.setHaveold("是");
                    else
                        user.setHaveold("否");
                    if (cbIsDisease.isChecked())
                        user.setHavedisease("是");
                    else
                        user.setHavedisease("否");
                    if (cbIsOther.isChecked())
                        user.setHavedisease("是");
                    changeInformation();
                    user = userDao.getUserByAccount(user.getAccount(),user.getPassword());
                    gotoNextActivity(MainActivity.class,user);
                }
            }
        };
        btnSaveInformation.setOnClickListener(listener);

        //绑定RadioGroup事件监听
        rgSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                r = findViewById(checkedId);
            }
        });

        //绑定复选框

    }

    private void changeInformation() {
        userDao = new UserDao2(this);
        if (etUsername.getText()!=null && !etUsername.getText().toString().equals("")){
            user.setUsername(etUsername.getText().toString());
        }
        if (etAge.getText()!=null && !etAge.getText().toString().equals("")){
            user.setAge(Integer.parseInt(etAge.getText().toString()));
        }
        if (rgSex.getCheckedRadioButtonId() != -1){
            user.setSex(r.getText().toString());
        }
        if (etNickname.getText()!=null && !etNickname.getText().toString().equals("")){
            user.setNickname(etNickname.getText().toString());
        }
        if (etIDCard.getText()!=null && !etIDCard.getText().toString().equals("")){
            user.setIdcard(etIDCard.getText().toString());
        }
        userDao.updateInformationById(user);
    }

    void initDate(){
        user = (User2) getIntent().getSerializableExtra("user");
    }
    /**
     * 跳转到下一个界面
     * @param activity -要跳转界面的类名
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    void gotoNextActivity(Class activity,User2 user){
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("user",user);
        finish();//此处一定要调用finish()方法
    }
}
